package com.xiaotu.dynamic;

import lombok.extern.slf4j.Slf4j;

import javax.annotation.PostConstruct;

/**
 * 数据源上下文
 *
 * @author zhangxiao
 * @create 2021-09-15 17:14
 */
@Slf4j
public class DynamicDataSourceContextHolder {

    public static final String MYSQL_KEY = "mysql";

    public static final String PGSQL_KEY = "pgsql";

    private static final ThreadLocal<String> DATA_SOURCE_HOLDER = new ThreadLocal<>();

    static {
        DATA_SOURCE_HOLDER.set(PGSQL_KEY);
    }

    public static String getDataSourceRouterKey() {
        return DATA_SOURCE_HOLDER.get();
    }

    public static void setDataSourceRouterKey(String key) {
        DATA_SOURCE_HOLDER.remove();
        DATA_SOURCE_HOLDER.set(key);
    }

}
